#include "hud_item_cpu.h"

extern int G_W, G_H;

HUD_ITEM_CPU::HUD_ITEM_CPU(CASH *cash):QObject(),QGraphicsItemGroup()
{

    this->cash=cash;

    init();
}

QPainterPath HUD_ITEM_CPU::shape() const
{
    return QPainterPath();
}

QRectF HUD_ITEM_CPU::boundingRect() const
{
    return QRectF();
}

void HUD_ITEM_CPU::init()
{
    init_borders();
    init_icon();
    init_score();


    QGraphicsDropShadowEffect *hud_shadow = new QGraphicsDropShadowEffect(this);
    hud_shadow->setBlurRadius(7);
    hud_shadow->setColor(Qt::blue);
    hud_shadow->setOffset(0);

    this->setGraphicsEffect(hud_shadow);

    score_points = 0;
}

void HUD_ITEM_CPU::init_borders()
{
    tl_anim_items = new QTimeLine(800, this);

    //item border down 1
    GRAPHICS_PIXMAP_ITEM *item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "cpu_border_down_1.svg"), this);
    QGraphicsItemAnimation *anim = new QGraphicsItemAnimation(this);

    anim->setItem(item);
    anim->setTimeLine(tl_anim_items);

    item->setPos(- G_W, - G_H);
    anim->setPosAt(0, QPointF(G_W - 106, G_H));
    anim->setPosAt(0.2, QPointF(G_W - 106, G_H - 22));

    //item border top 1
    item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "cpu_border_top_1.svg"), this);
    anim = new QGraphicsItemAnimation(this);

    anim->setItem(item);
    anim->setTimeLine(tl_anim_items);

    item->setPos(- G_W, - G_H);
    anim->setPosAt(0, QPointF(G_W, G_H - 90));
    anim->setPosAt(0.4, QPointF(G_W - 24, G_H - 90));

    //item border down 2
    item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "cpu_border_down_2.svg"), this);
    anim = new QGraphicsItemAnimation(this);

    anim->setItem(item);
    anim->setTimeLine(tl_anim_items);

    item->setPos(- G_W, - G_H);
    anim->setPosAt(0, QPointF(G_W - 155, G_H));
    anim->setPosAt(0.6, QPointF(G_W - 155, G_H - 22));

    //item border top 2
    item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "cpu_border_top_2.svg"), this);
    anim = new QGraphicsItemAnimation(this);

    anim->setItem(item);
    anim->setTimeLine(tl_anim_items);

    item->setPos(- G_W, - G_H);
    anim->setPosAt(0, QPointF(G_W, G_H - 150));
    anim->setPosAt(0.8, QPointF(G_W - 24, G_H - 150));

    //item border down 3
    item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "cpu_border_down_3.svg"), this);
    anim = new QGraphicsItemAnimation(this);

    anim->setItem(item);
    anim->setTimeLine(tl_anim_items);

    item->setPos(- G_W, - G_H);
    anim->setPosAt(0, QPointF(G_W - 205, G_H));
    anim->setPosAt(1, QPointF(G_W - 205, G_H - 22));
}

void HUD_ITEM_CPU::init_icon()
{
    tl_anim_icon = new QTimeLine(500, this);

    //item icon 1
    GRAPHICS_PIXMAP_ITEM *item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "cpu_icon_1.svg"), this);
    QGraphicsItemAnimation *anim = new QGraphicsItemAnimation(this);

    anim->setItem(item);
    anim->setTimeLine(tl_anim_icon);

    item->setPos(G_W - 53, G_H - 38);

    anim->setScaleAt(0, 1, 1);
    anim->setScaleAt(0.5, 1.3, 1.3);
    anim->setScaleAt(1, 1, 1);

    //item icon 2
    item = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "cpu_icon_2.svg"), this);
    anim = new QGraphicsItemAnimation(this);

    anim->setItem(item);
    anim->setTimeLine(tl_anim_icon);

    item->setPos(G_W - 43, G_H - 44);

    anim->setPosAt(0, item->pos());
    anim->setPosAt(0.5, QPointF(item->pos().x() + 20, item->pos().y()));
    anim->setPosAt(1, item->pos());

    tl_anim_icon->setEasingCurve(QEasingCurve::OutQuint);
}

void HUD_ITEM_CPU::init_score()
{

    int id = QFontDatabase::addApplicationFont("Res/fonts/Zekton.ttf");

    item_score = new GRAPHICS_TEXT_ITEM(this);
    item_score->setFont(QFont(QFontDatabase::applicationFontFamilies(id).first(), 25));
    item_score->setDefaultTextColor(QColor("#616ffc"));

    item_score->setPos(G_W + 1000, 0);

    item_score->setPlainText("00000");

    QGraphicsItemAnimation *anim_score = new QGraphicsItemAnimation(this);
    anim_score->setTimeLine(tl_anim_items);
    anim_score->setItem(item_score);

    anim_score->setPosAt(1, QPointF(G_W - 180, G_H - 70));
    anim_score->setPosAt(0, anim_score->posAt(1) + QPointF(G_W + 500, 0));
}

void HUD_ITEM_CPU::show_hud()
{
    tl_anim_items->stop();
    tl_anim_icon->stop();

    tl_anim_items->setDirection(QTimeLine::Forward);
    tl_anim_items->start();
    tl_anim_icon->start();

}

void HUD_ITEM_CPU::hide_hud()
{
    tl_anim_items->stop();
    tl_anim_icon->stop();

    tl_anim_items->setDirection(QTimeLine::Backward);
    tl_anim_items->start();
    tl_anim_icon->start();
}

void HUD_ITEM_CPU::add_score(int value)
{
    score_points += value;
    item_score->setPlainText(QString::number(score_points));
}

HUD_ITEM_CPU::~HUD_ITEM_CPU(){}
